package com.cls.business.service.impl;

import com.cls.business.entity.Review;
import com.cls.business.mapper.ReviewMapper;
import com.cls.business.service.IReviewService;
import com.cls.common.entity.QueryRequest;
import com.cls.business.entity.ExpertGroup;
import com.cls.business.mapper.ExpertGroupMapper;
import com.cls.business.service.IExpertGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 *  Service实现
 *
 * @author wandering
 * @date 2020-11-30 16:35:23
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ExpertGroupServiceImpl extends ServiceImpl<ExpertGroupMapper, ExpertGroup> implements IExpertGroupService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public PageInfo<ExpertGroup> findExpertGroups(QueryRequest request, ExpertGroup expertGroup) {
        // 申报课题ID
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ExpertGroup> list= baseMapper.getExpertGroupByTopicId(expertGroup.getTopicId());
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<ExpertGroup> findExpertGroups(ExpertGroup expertGroup) {
	    LambdaQueryWrapper<ExpertGroup> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        if(expertGroup.getPlanId() != null){
            queryWrapper.eq(ExpertGroup::getPlanId, expertGroup.getPlanId());
        }
        if(expertGroup.getExpertId() != null){
            queryWrapper.eq(ExpertGroup::getExpertId, expertGroup.getExpertId());
        }
        if(expertGroup.getTopicId() != null){
            queryWrapper.eq(ExpertGroup::getTopicId, expertGroup.getTopicId());
        }
        if(expertGroup.getGroupNo() != null){
            queryWrapper.eq(ExpertGroup::getGroupNo, expertGroup.getGroupNo());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createExpertGroup(ExpertGroup expertGroup) {
        this.save(expertGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateExpertGroup(ExpertGroup expertGroup) {
        this.saveOrUpdate(expertGroup);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteExpertGroups(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modifyExpertGroup(ExpertGroup expertGroup, Long expertId) {
        Long existExpertId = expertGroup.getExpertId();
        Long planId = expertGroup.getPlanId();
        List<Review> reviews;

        if(existExpertId != null){ // 已经分配专家ID，在该计划范畴内全局修改专家
            ExpertGroup param = new ExpertGroup();
            param.setPlanId(expertGroup.getPlanId());
            param.setExpertId(existExpertId);
            List<ExpertGroup> expertGroups = findExpertGroups(param);
            for(ExpertGroup one:expertGroups){
               one.setExpertId(expertId);
            }
            expertGroups.forEach(item->{
                saveOrUpdate(item);
            });
            reviews = reviewMapper.selectByPlanIdAndExpertId(planId, existExpertId);

        } else { // 未分配专家ID，只修改本组的专家信息
            expertGroup.setExpertId(expertId);
            updateExpertGroup(expertGroup);
            reviews = reviewMapper.selectByGroupId(expertGroup.getExpertGroupId());
        }

        for(Review review:reviews){
            review.setExpertId(expertId);
            reviewMapper.updateById(review);
        }
        return 0;
    }

    @Override
    public List<ExpertGroup> findByTopicIds(Set<Long> topicIds) {
        LambdaQueryWrapper<ExpertGroup> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        queryWrapper.in(ExpertGroup::getTopicId, topicIds);
        return this.baseMapper.selectList(queryWrapper);
    }

}
